// pages/deal_with/deal_with.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginBackFlag: false, //登录标记
    isPrize: null,
    id: '',
    ty: '',//扫码进来接收到type
    shopShareRecordId: '', //记录id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,'=====deal_with接收二维码参数')
    var scene = decodeURIComponent(options.scene)
    console.log(scene,'解析后的参数')
    let id = scene.split(",")[0].split(":")[1];
    let ty = scene.split(",")[1].split(":")[1];
    console.log(id, ty)
    let _this = this;
    //console.log(_this.data.isPrize == null)
    if (options.scene != undefined) {
      if (scene.split(",")[0].split(":")[0] == 'pri') {
        //console.log(ty)
        _this.setData({
          isPrize: true,
          shopShareRecordId: ty,
        })
      } else {
        _this.setData({
          id: id,
          ty: ty,
          isPrize: false
        })

      }

    }

    app.loadCartNum();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var thirdSession = wx.getStorageSync("third_Session");
    console.log('认证信息onShow:', typeof thirdSession);
    //授权返回
    console.log(this.data.loginBackFlag);
    if (this.data.loginBackFlag && !thirdSession) {
      app.loadCartNum();
    }
    let _this = this;
    if (thirdSession) {
      if (_this.data.isPrize) {
        _this.shareUser();
      } else {
        if (_this.data.ty == "POP") {//通过分享扫码进入店铺
           //去获取店铺id
          console.log('去获取店铺id', _this.data.id)
          Api.shareShopId(_this.data.id).then(res => {
            console.log(res)
            wx.reLaunch({
              url: `/pages/shopClass/shopClass?id=${res.data}&ty=${_this.data.ty}&shareRecordId=${_this.data.id}`
            });
          });
        } else if (_this.data.ty == "MCH") { //扫码进入店铺
          wx.reLaunch({
            url: `/pages/shopClass/shopClass?id=${_this.data.id}`,
          })
        }  
        else if (_this.data.ty == "DESC") { //进入普通商品
          wx.reLaunch({
            url: `/pages/details/shopDetail?id=${_this.data.id}`,
          })
        } else if (_this.data.ty == "GROUP") { //进入拼团
          wx.reLaunch({
            url: `/pages/details/spellDetail?id=${_this.data.id}`,
          })
        } else if (_this.data.ty == "LIMIT") { //进入秒杀
          wx.reLaunch({
            url: `/pages/details/spikeDetail?id=${_this.data.id}`,
          })
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  //分享id获取店铺id
  shareShopId() {
    console.log(this.data.isPrize, this.data.shopShareRecordId);
    let _this = this;
    Api.shareShopId(_this.data.shopShareRecordId).then(res => {
      console.log(res)
      wx.reLaunch({
        url: `/pages/shopClass/shopClass?id=${res.data}`,
        // url: `/pages/shopClass/shopClass?id=${res.data}&ty=${_this.data.ty}&shareRecordId=${_this.data.id}`
      });
    });
  },

  //分享者获取奖励
  shareUser() {
    let _this = this;
    Api.shareUser(_this.data.shopShareRecordId).then(res => {
      console.log("分享者获取奖励", res);
      wx.showToast({
        title: '分享者已获得奖励,快来分享吧!',
        icon: 'none',
        duration: 1500,
      })
      setTimeout(() => {
        _this.shareShopId();
      }, '1500');
    }).catch(err => {
      setTimeout(() => {
        _this.shareShopId();
      }, '700');
    });
  },


})